﻿
using System;

namespace DevelopersToolbox {
    public class HexEditorData {
        private byte[] _data;
        private int _dataLength;

        public HexEditorData(byte[] value) {
            _data = value ?? new byte[0];
            _dataLength = _data.Length;
        }

        public int Length {
            get { return _dataLength; }
        }

        public byte[] ToArray() {
            byte[] ret = new byte[_dataLength];
            Array.Copy(_data, ret, _dataLength);
            return ret;
        }

        private void EnsureDataSize(int length) {
            if (length >= _data.Length) {
                byte[] newData = new byte[length + 1000];
                Array.Copy(_data, newData, _data.Length);
                _data = newData;
            }
        }

        public byte this[int offset] {
            get { return _data[offset]; }
            set {
                EnsureDataSize(offset);
                _data[offset] = value;
                if (offset >= _dataLength) {
                    _dataLength = offset + 1;
                }
            }
        }
    }
}
